<?php

namespace plugin\ycMassage\app\model;

use Exception;
use plugin\ycMassage\utils\enum\ActionIdentity;
use plugin\ycMassage\utils\enum\State;
use think\facade\Db;

class MassageUserChannels extends Model
{
    public static function commission(MassageAppointmentService $model, $options)
    {
        $MassageUserChannels = MassageUserChannels::where(['id' => $options['channels_id'],'saas_appid'=>$model->saas_appid])->find();
        $channelsConfig = getHpConfig('', $model->saas_appid, 'channels');
        $commission=(int)$channelsConfig['commission'];
        if($MassageUserChannels->commission){
            $commission=(int)$MassageUserChannels->commission;
        }
        $commission = round($options['price'] * $commission / 100, 4);
        $platform_money=0;
        $channels_money=0;
        if($MassageUserChannels->salesman_uid){
            $MassageUserSalesman = MassageUserSalesman::where(['uid' => $MassageUserChannels->salesman_uid,'saas_appid'=>$model->saas_appid])->find();
            if($MassageUserSalesman&&$MassageUserSalesman->state===State::YES['value']){
                list($platform_money,$channels_money)=MassageUserSalesman::commission($MassageUserSalesman,$MassageUserChannels,[
                    'commission'=>$commission,
                    'price'=>$options['price'],
                    'trade'=>$options['trade'],
                    'orders_id'=>$model->orders_id,
                    'transaction_freeze_period'=>$options['transaction_freeze_period'],
                    'statistics' => $options['statistics'],
                ]);
            }
        }
        $remarks = "来自{$MassageUserChannels->title}订单号：{$options['trade']}";
        if ($options['transaction_freeze_period']) {
            $MassageUserWallet = MassageUserWallet::where(['uid' => $MassageUserChannels->uid])->lock(true)->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户钱包不存在');
            }
            $MassageUserFreezeMoney = new MassageUserFreezeMoney();
            $MassageUserFreezeMoney->uid = $MassageUserChannels->uid;
            $MassageUserFreezeMoney->form_uid = $model->id;
            $MassageUserFreezeMoney->saas_appid = $model->saas_appid;
            $MassageUserFreezeMoney->action = ActionIdentity::CHANNELS['value'];
            $MassageUserFreezeMoney->money = $commission-$channels_money;
            $MassageUserFreezeMoney->orders_id = $model->orders_id;
            $MassageUserFreezeMoney->state = State::NO['value'];
            $MassageUserFreezeMoney->unfreezing_time = date('Y-m-d H:i:s', strtotime("+{$options['transaction_freeze_period']} hours"));
            $MassageUserFreezeMoney->remarks = $remarks;
            $MassageUserFreezeMoney->save();
            $MassageUserWallet->channels_freeze_money = Db::raw('channels_freeze_money+' . ($commission-$channels_money));
            $MassageUserWallet->save();
        } else {
            MassageUserChannelsBill::increase($remarks, $MassageUserChannels->uid, $commission-$channels_money, $model->id);
        }
        $MassageUserChannelsStatistics = MassageUserChannelsStatistics::where(['saas_appid' => $model->saas_appid, 'channels_id' => $MassageUserChannels->id])->whereDay('create_time')->find();
        if (!$MassageUserChannelsStatistics) {
            $MassageUserChannelsStatistics = new MassageUserChannelsStatistics;
            $MassageUserChannelsStatistics->uid = $MassageUserChannels->uid;
            $MassageUserChannelsStatistics->saas_appid = $model->saas_appid;
            $MassageUserChannelsStatistics->channels_id = $MassageUserChannels->id;
        }
        foreach ($options['statistics'] as $key => $value) {
            if ($value) {
                $MassageUserChannelsStatistics->{$key} = Db::raw("{$key}+{$value}");
            }
        }
        $MassageUserChannelsStatistics->commission = Db::raw("commission+".($commission-$channels_money));
        $MassageUserChannelsStatistics->save();
        if($commission<=0&&$platform_money<=0){
            return 0;
        }
        $platform_commission=(int)$channelsConfig['platform_commission'];
        if($MassageUserChannels->platform_commission){
            $platform_commission=(int)$MassageUserChannels->platform_commission;
        }
        return $platform_money+$commission-round($commission*$platform_commission/100,4);
    }
}
